/*
 * $Id$
 *
 * Copyright 2008 HuangHuang. All rights reserved.
 */

package com.crm.service.impl;

import com.crm.service.IContactService;
import com.crm.service.ISloggerService;
import com.crm.dao.IContactDao;
import com.crm.dao.IProcessDao;
import com.crm.model.Contact;
import com.crm.model.User;
import com.crm.model.SysLog;

import java.util.List;

/**
 * @author <a href="mailto:huang0510@hotmail.com">HuangHuang</a>
 * @version $Id$
 */
public class ContactService implements IContactService {

    private static final String INF_CRE = "Create Contact";
    private static final String INF_DEL = "Delete Contact";
    private static final String INF_UPD = "Update Contact";
    private static final String INF_PROC_DEL = "Delete Process";
    private IContactDao contactDao;
    private ISloggerService slogServ;
    private IProcessDao processDao;

    public void setContactDao(IContactDao contactDao) {
        this.contactDao = contactDao;
    }

    public void setSlogServ(ISloggerService slogServ) {
        this.slogServ = slogServ;
    }

    public void setProcessDao(IProcessDao processDao) {
        this.processDao = processDao;
    }

    /**
     * {@inheritDoc}
     */
    public void create(Contact contact) {
        contactDao.saveOrUpdate(contact);
        slogServ.create(contact.getUser(), SysLog.T_CONT, SysLog.ACT_C, INF_CRE);
    }

    /**
     * {@inheritDoc}
     */
    public void update(Contact contact) {
        contactDao.saveOrUpdate(contact);
        slogServ.create(contact.getUser(), SysLog.T_CONT, SysLog.ACT_U, INF_UPD);
    }

    /**
     * {@inheritDoc}
     */
    public List getContacts() {
        return contactDao.getContactAll();
    }

    /**
     * {@inheritDoc}
     */
    public boolean isExistContact(String currentName) {
        List contactNameList = contactDao.getContactNameAll();
        for (Object aContactNameList : contactNameList) {
            String existName = (String) aContactNameList;
            if (existName.trim().toLowerCase().equals(currentName)) {
                return false;
            }
        }
        return true;
    }

    /**
     * {@inheritDoc}
     */
    public List getContactsByUser(User user) {
        String ug = user.getUserGroup();
        if (User.GROUP_SUPER.equals(ug) || User.GROUP_ADMIN.equals(ug)) {
            return contactDao.getContactAll();
        } else {
            return contactDao.getContactsByUsercode(user.getUserCode());
        }
    }

    /**
     * {@inheritDoc}
     */
    public List getContactByCustomerId(User user, String customerId) {
        checkUser(user);
        if (User.GROUP_SUPER.equals(user.getUserGroup())) {
            return contactDao.getContactByCustomerId(customerId);
        } else {
            return contactDao.getContactByCustomerId(user.getUserCode(), customerId);
        }
    }

    public Contact getContactById(String contactId) {
        return contactDao.getContactById(contactId);
    }

    /**
     * {@inheritDoc}
     */
    public List getContacts(User user, String[] searchParams) {
        checkUser(user);
        List list;
        String info = "search contact without keywords.";
        if (searchParams[0] != null && searchParams[1].equals("所属客户")) {
            list = contactDao.getContactsByKeywords(user.getUserGroup(), user.getUserCode(), searchParams[0], "", "");
            info = "search contact by customer. keyword: " + searchParams[0];
        } else if (searchParams[0] != null && searchParams[1].equals("联系人")) {
            list = contactDao.getContactsByKeywords(user.getUserGroup(), user.getUserCode(), "", searchParams[0], "");
            info = "search contact by name, keyword: " + searchParams[0];
        } else if (searchParams[0] != null && searchParams[1].equals("邮箱")) {
            list = contactDao.getContactsByKeywords(user.getUserGroup(), user.getUserCode(), "", "", searchParams[0]);
            info = "search contact by email, keyword: " + searchParams[0];
        } else {
            list = contactDao.getContactsByKeywords(user.getUserGroup(), user.getUserCode(), "", "", "");
        }
        slogServ.create(user, SysLog.T_CONT, SysLog.ACT_R, info);
        return list;
    }

    /**
     * {@inheritDoc}
     */
    public void delete(Contact contact, User user) {
        int c = processDao.deleteAll(contact.getId());
        contactDao.delete(contact);
        while(c > 0) {
            slogServ.create(user, SysLog.T_PROC, SysLog.ACT_D, INF_PROC_DEL);
            c--;
        }
        slogServ.create(user, SysLog.T_CONT, SysLog.ACT_D, INF_DEL);
    }

    public int count(String customerId) {
        return contactDao.count(customerId);
    }

    public List getRecycleCont(String customerId) {
        return contactDao.getRecycleCont(customerId);
    }

    private void checkUser(User user) {
        if (user.getUsername() == null || user.getPassword() == null) {
            throw new NullPointerException("User session is null!");
        }
    }
}
